unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ImgList, ExtCtrls, StdCtrls;

type
  TfmEMCLauncher = class(TForm)
    lvRun: TListView;
    ImageList1: TImageList;
    Image1: TImage;
    Panel1: TPanel;
    cbDebugOpt: TCheckBox;
    Button1: TButton;
    procedure lvRunDblClick(Sender: TObject);
    procedure cbDebugOptClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure ReloadList;
  public
    { Public declarations }
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
  end;

var
  fmEMCLauncher: TfmEMCLauncher;

implementation

{$R *.dfm}

uses uFormReg, uBaseCalcForm, uLocalization, uConfig, uLangSelectForm;

function RunProgramA(AExeFile, AParams: string): boolean;
var startInfo: TStartupInfo;
    procInfo: TProcessInformation;
    cdir, cmdline: string;
begin
  ZeroMemory(@startInfo, sizeOf(TStartupInfo));
  startInfo.cb := sizeOf(TStartupInfo);

  if AnsiPos(' ',AExeFile)>0 then cmdline := '"'+AExeFile+'"'
  else cmdline := AExeFile;
  if (AParams<>'') then cmdLine := cmdLine+' '+AParams;

  cdir := ExtractFilePath(AExeFile);
  Result := CreateProcessA(nil, PAnsiChar(cmdline), nil, nil, false, NORMAL_PRIORITY_CLASS, nil, PAnsiChar(cdir), startInfo, procInfo);
end;

procedure _enum(const ACalcClass: TBaseCalcFormClass; AUser: pointer); stdcall;
var tmp: string;
    ic: TIcon;
begin
  with TfmEMCLauncher(AUser) do begin
    tmp := AppDir+'Data\ico_'+ACalcClass.RunKey+'.ico';

    if (not ACalcClass.IsDebug) or (ACalcClass.IsDebug and cbdebugOpt.Checked) then
    with lvRun.Items.Add do begin
      Caption := GetString('#'+ACalcClass.RunKey+'\title', false);
      if (Caption='') then
        Caption := ACalcClass.CalcFriendlyName;
      SubItems.Add(ACalcClass.RunKey);

      if (ACalcClass.IsDebug) then
        ImageIndex := 0
      else begin
        ImageIndex := 1;
        if FileExists(tmp) then begin
          ic := TIcon.Create;
          try
            ic.LoadFromFile(tmp);
            ImageIndex := ImageList1.AddIcon(ic);
            ic.Free;
          except end;
        end;
      end;
    end;
  end;
end;

{ TfmEMCLauncher }

procedure TfmEMCLauncher.AfterConstruction;
begin
  inherited;
  cbDebugOpt.Caption := GetString('#global\showdebug', false);
  if (cbDebugOpt.Caption='') then cbDebugOpt.Caption := 'Show debug modules';
  ReloadList;

  Width := Config.AsInteger['window.launcher.width', Self.Width];
  Height := Config.AsInteger['window.launcher.height', Self.Height];
  
  //lvRun.Realign;
  Self.Realign;
end;

procedure TfmEMCLauncher.lvRunDblClick(Sender: TObject);
var pf: TBaseCalcFormClass;
begin
  if lvRun.Selected<>nil then begin
    pf := FindCalcByKey(lvRun.Selected.SubItems[0]);
    if (pf<>nil) then // pf.InvokeCalc;
      RunProgramA(Application.ExeName, lvRun.Selected.SubItems[0]);
  end;
end;

procedure TfmEMCLauncher.cbDebugOptClick(Sender: TObject);
begin
  if (cbDebugOpt.Checked) then messageBox(Handle, 'Debug versions of calculators may show incorrect results!','Warning', MB_ICONINFORMATION or MB_OK);
  lvRun.Clear;
  EnumCalcClasses(_enum, Self);
end;

procedure TfmEMCLauncher.BeforeDestruction;
begin
  Config.AsInteger['window.launcher.width', 0] := Self.Width;
  Config.AsInteger['window.launcher.height', 0] := Self.Height;
  inherited;
end;

procedure TfmEMCLauncher.Button1Click(Sender: TObject);
begin
  Config.AsString['main.language',''] := TfmLangSelect.Invoke;
  AutoLoadLocale;
  ReloadList;
end;

procedure TfmEMCLauncher.ReloadList;
begin
  ImageList1.Clear;
  ImageList1.AddIcon(Application.Icon);
  lvRun.Clear;
  EnumCalcClasses(_enum, Self);
end;

end.
